package com.wanxi.enterprise.service.impl;

import com.wanxi.enterprise.api.dto.EnterpriseUpdateInDTO;
import com.wanxi.enterprise.api.dto.LocateDataFindByIdOutDTO;
import com.wanxi.enterprise.api.entity.*;
import com.wanxi.enterprise.api.feign.EnterpriseFeignCilent;
import com.wanxi.enterprise.api.query.ShopQuery;
import com.wanxi.enterprise.mapper.EnterpriseMapper;
import com.wanxi.enterprise.api.query.EnterpriseQuery;
import com.wanxi.enterprise.api.query.LocateApplyQuery;
import com.wanxi.enterprise.api.result.CommonResult;
import com.wanxi.enterprise.api.vo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;

@RestController
public class EnterpriseServiceApiImpl implements EnterpriseFeignCilent {

    //注入EnterpriseMapper
    @Resource
    private EnterpriseMapper enterpriseMapper;

    private static Logger log = LoggerFactory.getLogger(EnterpriseServiceApiImpl.class);

    //注入发件人邮箱
    @Resource
    private JavaMailSender mailSender;

    /**
     * @Author jinfang
     * @Description // 管理员查看所有商家
     * @Date  2021/1/6
     * @Params EnterpriseQuery
     * @return CommonResult
     **/

    @Override
    @GetMapping("admin/findEnterpriseAll")
    public CommonResult<List<EnterpriseFindAllOutVo>> findAll(EnterpriseQuery enterpriseQuery) {
        String message = "";
        try {
            //属性拷贝到Entity类
            EnterpriseQueryEntity enterpriseQueryEntity = new EnterpriseQueryEntity();
            BeanUtils.copyProperties(enterpriseQuery,enterpriseQueryEntity);
            //数据库去查找
            List<EnterpriseFindAllOutVo> list = enterpriseMapper.findAll(enterpriseQueryEntity);
            return CommonResult.success(list);
        } catch (Exception e) {
            message = e.getMessage();
            log.info(message);
        }
        return CommonResult.failed(message);
    }

    @Override
    @GetMapping("admin/findApplyAll")
    public CommonResult<List<LocateApplyFindAllOutVO>> findApplyAll(LocateApplyQuery locateApplyQuery) {
        String message = "";
        try {
            //属性拷贝到Entity类
            LocateApplyQureyEntity locateApplyQureyEntity = new LocateApplyQureyEntity();
            BeanUtils.copyProperties(locateApplyQuery,locateApplyQureyEntity);
            //去数据库查询
            List<LocateApplyFindAllOutVO> list = enterpriseMapper.findApplyAll(locateApplyQureyEntity);
            return CommonResult.success(list);
        } catch (BeansException e) {
            message = e.getMessage();
            log.info(message);
        }
        return CommonResult.failed(message);
    }

    /**
     * @Author jinfang
     * @Description // 管理员收到入驻申请，添加商家资料到入驻申请表
     * @Date  2021/1/6
     * @Params LocateApplyAddInVo
     * @return CommonResult
     **/

    @Override
    @PostMapping("enterprise/addApply")
    public CommonResult addApply(LocateApplyAddInVo locateApplyAddInVo) {
        String message = "";
        try {
            LocateApply locateApply = new LocateApply();
            //属性拷贝到Entity类
            BeanUtils.copyProperties(locateApplyAddInVo,locateApply);
            //去数据库添加记录
            Long aLong = enterpriseMapper.addApply(locateApply);
            return CommonResult.success(aLong);
        } catch (BeansException e) {
            message = e.getMessage();
            log.info(message);
        }
        return CommonResult.failed(message);
    }


    /**
     * @Author jinfang
     * @Description // 商家完善自己的商家信息（从游客角色转换为商家）
     * @Date  2021/1/6
     * @Params EnterpriseAddInVo
     * @return CommonResult
     **/
    @Override
    @PostMapping("enterprise/addEnterprise")
    public CommonResult addEnterprise(EnterpriseAddInVo enterpriseAddInVo) {
        String message = "";
        try {
            EnterpriseEntity enterpriseEntity = new EnterpriseEntity();
            BeanUtils.copyProperties(enterpriseAddInVo,enterpriseEntity);
            Long aLong = enterpriseMapper.addEnterprise(enterpriseEntity);
            Long alongAgin = enterpriseMapper.addUserInf(enterpriseEntity);
            return CommonResult.success(aLong,alongAgin);
        } catch (BeansException e) {
            message = e.getMessage();
            log.info(message);
        }
        return CommonResult.failed(message);
    }

    /**
     * @Author jinfang
     * @Description // 根据id查看商家信息
     * @Date  2021/1/6
     * @Params Long id
     * @return CommonResult
     **/

    @Override
    @GetMapping("enterprise/findApplyDataById")
    public CommonResult<LocateDataFindByIdOutDTO> findApplyDataById(Long id) {
        String message = "";
        try {
            LocateDataFindByIdOutDTO locateDataFindByIdOutDTO = enterpriseMapper.findApplyDataById(id);
            return CommonResult.success(locateDataFindByIdOutDTO);
        } catch (Exception e) {
            message = e.getMessage();
            log.info(message);
        }
        return CommonResult.failed(message);
    }

    /**
     * @Author jinfang
     * @Description // 商家根据id修改自己的信息
     * @Date  2021/1/6
     * @Params Long id
     * @return CommonResult
     **/

    @Override
    @PutMapping("enterprise/updateEnterpriseById")
    public CommonResult updateEnterpriseById(EnterpriseUpdateInDTO enterpriseUpdateInDTO) {
        Long along = enterpriseMapper.updateEnterpriseById(enterpriseUpdateInDTO);
        return CommonResult.success(along);
    }

    /**
     * @Author jinfang
     * @Description // 管理员点击审核通过，并把数据生成至入驻资料表
     * @Date  2021/1/6
     * @Params EnterpriseUpdateInDTO
     * @return CommonResult
     **/
    @Override
    @PutMapping("admin/addApplyData")
    @Transactional
    public CommonResult updateApply(LocateApplyAuditInVo locateApplyAuditInVo) {
        //属性拷贝到Entity类
        LocateApply locateApply = new LocateApply();
        BeanUtils.copyProperties(locateApplyAuditInVo,locateApply);
        //获得商家id
        Long findId = locateApply.getEnterpriseId();
        LocateApply locateApplyCheck = enterpriseMapper.checkLocateApply(findId);
        //判断必填的字段是否有内容,审核状态要为0
        if (locateApplyCheck.getEnterpriseName()!=null && !"".equals(locateApplyCheck.getEnterpriseName())
            && locateApplyCheck.getEnterpriseCity() != null && !"".equals(locateApplyCheck.getEnterpriseCity())
            && locateApplyCheck.getLegalRepresentativeName() != null && !"".equals(locateApplyCheck.getLegalRepresentativeName())
            && locateApplyCheck.getIdCardNumber() != null && !"".equals(locateApplyCheck.getIdCardNumber())
            && locateApplyCheck.getIdCardType() != null && !"".equals(locateApplyCheck.getIdCardType())
            && locateApplyCheck.getBusinessLicense() != null && !"".equals(locateApplyCheck.getBusinessLicense())
            && locateApplyCheck.getTaxRegistration() != null && !"".equals(locateApplyCheck.getTaxRegistration())
            && locateApplyCheck.getOrganizationCode() != null && !"".equals(locateApplyCheck.getOrganizationCode())
            && locateApplyCheck.getOpeningBankLicense() != null && !"".equals(locateApplyCheck.getOpeningBankLicense())
            && locateApplyCheck.getBankAccount() != null && !"".equals(locateApplyCheck.getBankAccount())
            && locateApplyCheck.getFoodBusinessLicense() != null && !"".equals(locateApplyCheck.getFoodBusinessLicense())
            && locateApplyCheck.getQualityInspectionReport() != null && !"".equals(locateApplyCheck.getQualityInspectionReport())
            && locateApplyCheck.getLocateStatus()==0) {
            //获得商家id
            Long id = locateApply.getEnterpriseId();
            //修改入驻状态
            Boolean flag = enterpriseMapper.updateApply(locateApply);
            //如果更新入驻状态成功，把数据从申请表转存到入驻表
            if (flag){
                //根据商家id查询到入驻申请表的信息
                LocateDataFindByIdOutDTO locateDataFindByIdOutDTO = enterpriseMapper.findLocateApplyById(id);
                //将按照id查询到的数据放到入驻资料表
                LocateDataEntity locateDataEntity = new LocateDataEntity();
                BeanUtils.copyProperties(locateDataFindByIdOutDTO,locateDataEntity);
                Long along = enterpriseMapper.addApplyData(locateDataEntity);
                return CommonResult.success(along);
            }
        }
        return CommonResult.failed("商家输入的数据不全，审核失败");

    }

    /**
     * @Author jinfang
     * @Description // 管理员点击审核驳回，并且通过邮箱方式返回驳回理由给商家
     * @Date  2022/1/13
     * @Params locateApplyAuditInVo
     * @return CommonResult
     **/
    @Override
    @Transactional
    @GetMapping("admin/noApply")
    public CommonResult noApply(NoLocateApplyInVO noLocateApplyInVO) {
        String msg = "";
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            //平台发送方邮箱
            message.setFrom("1426791993@qq.com");
            //接收者邮箱，根据商家id查询对应的商家邮箱
            Long id = noLocateApplyInVO.getEnterpriseId();
            String enterpriseEamil = enterpriseMapper.findEnterpriseEmail(id);
            //商家接受方邮箱
            message.setTo(enterpriseEamil);
            //主题
            message.setSubject("驳回理由");
            //内容
            message.setText(noLocateApplyInVO.getRejected());
            mailSender.send(message);
            return CommonResult.success("已驳回，通过邮箱通知商家");
        } catch (MailException e) {
            msg = e.getMessage();
            log.info(msg);
        }
        return CommonResult.failed(msg);
    }

    /**
     * @Author jinfang
     * @Description // 管理员更新商家入驻状态
     * @Date  2021/1/20
     * @Params id
     * @return CommonResult
     **/
    @Override
    @PutMapping("admin/updateLocateStatus")
    public CommonResult updateLocateStatus(NoLocateApplyInVO noLocateApplyInVO) {
        String message = "";
        try {
            Long id = noLocateApplyInVO.getEnterpriseId();
            //更改enterprise的入驻状态
            int twoId = enterpriseMapper.updateEnterpriseLocateStatus(id);
            return CommonResult.success(twoId);
        } catch (BeansException e) {
            message = e.getMessage();
            log.info(message);
        }

        return CommonResult.failed(message);
    }

    /**
     * @Author jinfang
     * @Description // 商家添加店铺
     * @Date  2021/1/10
     * @Params shopAddInVo
     * @return CommonResult
     **/
    @Override
    @PostMapping("enterprise/addShop")
    public CommonResult addShop(ShopAddInVo shopAddInVo) {
        String message = "";
        try {
            //封装成Entity
            ShopEntity shopEntity = new ShopEntity();
            BeanUtils.copyProperties(shopAddInVo,shopEntity);
            //去数据库添加店铺
            int id = enterpriseMapper.addShop(shopEntity);
            return CommonResult.success(id);
        } catch (BeansException e) {
            message = e.getMessage();
            log.info(message);
        }

        return CommonResult.failed(message);
    }

    /**
     * @Author jinfang
     * @Description // 商家查看旗下的店铺列表
     * @Date  2021/1/10
     * @Params shopQuery
     * @return CommonResult
     **/
    @Override
    @GetMapping("enterprise/findShopAll")
    public CommonResult<List<ShopFindAllOutVo>> findShopAll(ShopQuery shopQuery) {
        String message = "";
        try {
            //封装成Entity
            ShopEntity shopEntity = new ShopEntity();
            BeanUtils.copyProperties(shopQuery,shopEntity);
            //去数据库查找店铺
            List<ShopFindAllOutVo> shopList = enterpriseMapper.findShopAll(shopEntity);

            return CommonResult.success(shopList);
        } catch (BeansException e) {
            message = e.getMessage();
            log.info(message);
        }
        return CommonResult.failed(message);
    }

    /**
     * @Author jinfang
     * @Description // 商家根据id查看店铺列表
     * @Date  2021/1/10
     * @Params shopQuery
     * @return CommonResult
     **/
    @Override
    @GetMapping("enterprise/findShopById")
    public CommonResult<ShopFindByIdOutVo> findShopById(Long id) {
        String message = "";
        try {
            //去数据库根据id查找
            ShopFindByIdOutVo shopFindByIdOutVo = enterpriseMapper.findShopById(id);
            return CommonResult.success(shopFindByIdOutVo);
        } catch (BeansException e) {
            message = e.getMessage();
            log.info(message);
        }
        return CommonResult.failed(message);
    }

    /**
     * @Author jinfang
     * @Description // 商家修改店铺信息
     * @Date  2021/1/10
     * @Params shopUpdateInVo
     * @return CommonResult
     **/
    @Override
    @PutMapping("enterprise/updateShopById")
    public CommonResult updateShop(ShopUpdateInVo shopUpdateInVo) {
        String message = "";
        try {
            //封装成Entity
            ShopEntity shopEntity = new ShopEntity();
            BeanUtils.copyProperties(shopUpdateInVo,shopEntity);
            //去数据库修改店铺
            int deleteId = enterpriseMapper.updateShop(shopEntity);
            return CommonResult.success(deleteId);
        } catch (BeansException e) {
            message = e.getMessage();
            log.info(message);
        }
        return CommonResult.failed(message);
    }

    /**
     * @Author jinfang
     * @Description // 商家逻辑删除店铺
     * @Date  2021/1/10
     * @Params id
     * @return CommonResult
     **/
    @Override
    @PutMapping("enterprise/deleteShopById")
    public CommonResult deleteShopById(Long id) {
        String message = "";
        try {
            int deleteId = enterpriseMapper.deleteShopById(id);
            return CommonResult.success(deleteId);
        } catch (BeansException e) {
            message = e.getMessage();
            log.info(message);
        }
        return CommonResult.failed(message);
    }

}
